//
//  LFMainViewController.swift
//  SDAutoWeibo
//
//  Created by 刘健 on 16/3/19.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit

class LFMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //自定义 tabBar
        // 因为 tabBar 属性 只读 所以 使用kvc 赋值
        let bar = LFTabBar()
        self.setValue(bar, forKeyPath: "tabBar")
        
        //自定义按钮点击事件回调
        bar.didComposeButton = {[weak self] () ->() in
            
          //使用字符串创建控制器 swift 里面 要使用 产品名.控制器名
            let bundleName = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as? String ?? ""
            
            let VCName = NSClassFromString("\(bundleName).LFComposeViewController") as! UIViewController.Type
            
            let cpVC = VCName.init()
            
//            let cpVC = LFComposeViewController()
            
          self!.presentViewController(UINavigationController(rootViewController: cpVC), animated: true, completion: nil)
        }
        
        
        
        
        let homeVC = LFHomeTableViewController()
      addChildViewControllerWithImageNameAndTitle(homeVC, imageName: "tabbar_home", title: "首页")
        
        let discoverVC = LFDiscoverTableViewController()
        addChildViewControllerWithImageNameAndTitle(discoverVC, imageName: "tabbar_discover", title: "发现")
        
        let messageVC = LFMessageTableViewController()
        addChildViewControllerWithImageNameAndTitle(messageVC, imageName: "tabbar_message_center", title: "信息")
        let profileVC = LFProfileTableTableViewController()
        addChildViewControllerWithImageNameAndTitle(profileVC, imageName: "tabbar_profile", title: "我")
    }
    
    
    func addChildViewControllerWithImageNameAndTitle(controller: UIViewController, imageName: String , title: String) {
        
        controller.title = title
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        let selectImageName = imageName + "_highlighted"
        //总是显示图片原型
        controller.tabBarItem.selectedImage = UIImage(named:selectImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        controller.tabBarItem.image = UIImage(named: imageName)
        
        addChildViewController(LFNavViewController(rootViewController: controller))
      
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
